/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import controller.EntropieRechner;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import model.Knoten;
import model.Tabelle;

/**
 *
 * @author Max
 */
public class TabellenInteraktiv extends javax.swing.JFrame {
    
    private DefaultTableModel model;
    
    private boolean istAktiv = true;
    
    private int index=0;
    
    private BaumInteraktiv parentFrame;
    
    private Tabelle tabelle;

    /**
     * Creates new form TabellenInteraktiv
     */
    public TabellenInteraktiv(DefaultTableModel model, BaumInteraktiv parentFrame) {
        this.parentFrame = parentFrame;
        this.model = model;
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        jTable2.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jBAuswaehlen = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        jButton1.setText(org.openide.util.NbBundle.getMessage(TabellenInteraktiv.class, "TabellenInteraktiv.jButton1.text")); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jBAuswaehlen.setText(org.openide.util.NbBundle.getMessage(TabellenInteraktiv.class, "TabellenInteraktiv.jBAuswaehlen.text")); // NOI18N
        jBAuswaehlen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAuswaehlenActionPerformed(evt);
            }
        });
        jPanel2.add(jBAuswaehlen, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBAuswaehlenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAuswaehlenActionPerformed
        // TODO add your handling code here:
        if (jTable2.getSelectedColumn() != model.getColumnCount() -1 && jTable2.getSelectedColumn() != -1) {
            istAktiv = false;
            index = jTable2.getSelectedColumn();
            parentFrame.getIndexListe().add(new Integer(index));
            parentFrame.setEnabled(true);
            Knoten knoten = new Knoten(jTable2.getColumnName(index), 2, parentFrame.getWurzel());
            knoten.setTabelle(tabelle);
            knoten.setIndex(index);
            EntropieRechner entropieRechner = new EntropieRechner(tabelle.getSpaltenListe().get(index), tabelle.getSpaltenListe().get(tabelle.getSpaltenListe().size()-1));
            knoten.getTabelle().getSpaltenListe().get(index).setEntropie(entropieRechner.calcEntropie());
            knoten.setEntropie(knoten.getTabelle().getSpaltenListe().get(index).getEntropie());
            knoten.setTupelListe(knoten.getTabelle().getSpaltenListe().get(index).getTupelListe());
            parentFrame.zeichneNeuenKnoten(parentFrame.getWurzel(), knoten, this.getTitle());
            if (knoten.getEntropie() == 0) {
                for (int i = 0; i < knoten.getTupelListe().size(); i++) {
                    parentFrame.zeichneNeuenKnoten(knoten, new Knoten(knoten.getTupelListe().get(i).getDaten()[1], i), knoten.getTupelListe().get(i).getDaten()[0]);
                }
            }
            dispose();
        }
    }//GEN-LAST:event_jBAuswaehlenActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAuswaehlen;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables

    /**
     * @param model the model to set
     */
    public void setModel(DefaultTableModel model) {
        this.model = model;
    }

    /**
     * @return the istAktiv
     */
    public boolean isIstAktiv() {
        return istAktiv;
    }

    /**
     * @return the index
     */
    public int getIndex() {
        return index;
    }

    /**
     * @param tabelle the tabelle to set
     */
    public void setTabelle(Tabelle tabelle) {
        this.tabelle = tabelle;
    }

}
